<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/12/5
 * Time: 17:09
 */

namespace app\api\controller;


use think\Db;

class Notice extends Base
{
    private $user_id;
    private $page;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 系统/个人消息列表
     * @return \think\response\Json
     * notice_type   1 系统消息   2 个人消息
     */
    public function noticeList(){
        $type = input('notice_type');
        $list = $this->allNotice($type);
        if($list){
            // 已读/未读
            foreach($list as $k => $v){
                if(strpos($v['notice_read_user'], ',' . $this->user_id . ',') !== false){
                    $list[$k]['is_read'] = 1; // 已读
                }else{
                    $list[$k]['is_read'] = 2; // 未读
                }
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 消息展示的第一条
     * @return \think\response\Json
     */
    public function noticeOne(){
        // 系统消息
        $one_system_notice = $this->allNotice(1);
        if($one_system_notice){
            $system_notice = $one_system_notice[0];
        }else{
            $system_notice = [];
        }
        // 个人消息
        $one_person_notice = $this->allNotice(2);
        if($one_person_notice){
            $person_notice = $one_person_notice[0];
        }else{
            $person_notice = [];
        }
        $list['system_notice'] = $system_notice;
        $list['person_notice'] = $person_notice;
        return json(['status' => 200, 'msg' => $list]);
    }
    
    /**
     * 清空消息
     * @return \think\response\Json
     */
    public function delNotice(){
        $delList = $this->allNotice(3);
        if($delList){
            Db::startTrans();
            try{
                foreach($delList as $k => $v){
                    $del_user = $v['notice_del_user'] . $this->user_id . ',';
                    Db::table('up_notice')->where('notice_id', $v['notice_id'])->setField('notice_del_user', $del_user);
                }
                Db::commit();
                return json(['status' => 200, 'msg' => '操作成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['status' => 0, 'msg' => '操作失败']);
            }
        }else{
            return json(['status' => 0, 'msg' => '没有需要清空的消息']);
        }
    }
    
    /**
     * 消息详情
     * @return \think\response\Json
     * notice_id   消息ID
     */
    public function noticeDetail(){
        $notice_id = input('notice_id');
        $info = Db::table('up_notice')
            ->field('notice_title, notice_thumb, notice_content, notice_time, notice_read_user')
            ->where('notice_id', $notice_id)
            ->find();
        if(strpos($info['notice_read_user'], ',' . $this->user_id . ',') === false){
            $read_user = $info['notice_read_user'] . $this->user_id . ',';
            Db::table('up_notice')->where('notice_id', $notice_id)->setField('notice_read_user', $read_user);
        }
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 是否有未读消息
     * @return \think\response\Json
     * n_type     1 全部消息    2 系统消息   3 个人消息
     */
    public function redPoint(){
        $n_type = input('n_type');
        if($n_type == 1){ // 全部
            $system = $this->noReadNotice(1);
            $person = $this->noReadNotice(2);
            if($system != 0 && $person != 0){ // 有未读
                $num = $system + $person;
            }else{
                $num = -1;
            }
        }elseif($n_type == 2){ // 系统
            $system = $this->noReadNotice(1);
            if($system != 0){ // 有未读
                $num = $system;
            }else{
                $num = -1;
            }
        }else{
            $person = $this->noReadNotice(2);
            if($person != 0){ // 有未读
                $num = $person;
            }else{
                $num = -1;
            }
        }
        return json(['status' => 200, 'msg' => $num]);
    }
    
    /**
     * 消息列表-----只做查询，不涉及逻辑
     * @param $notice_type    1 系统消息   2 个人消息   3 清空消息时传3
     * @return false|\PDOStatement|string|\think\Collection
     */
    private function allNotice($notice_type){
//        $where['notice_user'] = ['like', '%,' . $this->user_id . ',%']; // 备用
        $where['notice_del_user'] = ['notlike', '%,' . $this->user_id . ',%'];
        if($notice_type != 3){
            $where['notice_type'] = $notice_type;
        }
        if($notice_type != 1){
            $where['notice_user'] = [['like', '%,' . $this->user_id . ',%'], ['=', '-1'], 'or'];
        }
    
        $list = Db::table('up_notice')
            ->where($where)
            ->field('notice_id, notice_title, notice_thumb, notice_read_user, notice_time, 
            notice_del_user, notice_cate, notice_subject_id');
        // 清空消息时，不需要分页和排序
        if($notice_type != 3){
            $list->page($this->page, 10)->order('notice_time desc');
        }
        $list = $list->select();
        return $list;
    }
    
    /**
     * 未读消息
     * @param $notice_type    1 系统消息   2 个人消息
     * @return false|\PDOStatement|string|\think\Collection
     */
    private function noReadNotice($notice_type){
        $where['notice_read_user'] = ['notlike', '%,' . $this->user_id . ',%']; // 未读
        $where['notice_del_user'] = ['notlike', '%,' . $this->user_id . ',%']; // 未删除
        if($notice_type == 1){ // 系统消息
            $where['notice_type'] = 1;
        }else{ // 个人
            $where['notice_type'] = 2;
            $where['notice_user'] = [['like', '%,' . $this->user_id . ',%'], ['=', '-1'], 'or'];
        }
        $num = Db::table('up_notice')
            ->where($where)
            ->field('notice_id')
            ->count();
        return $num;
    }
}